package com.situ.mall.thread.juc;


import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanExample {
    private static AtomicBoolean OPEN_OR_CLOSE = new AtomicBoolean(false);

    public static void main(String[] args) throws InterruptedException {
        //期望保险柜是关闭的false
        boolean expect = OPEN_OR_CLOSE.get();
        boolean update = true;
        Thread zhangsan = new Thread(new Runnable() {
            @Override
            public void run() {
                boolean isOpen = OPEN_OR_CLOSE.compareAndSet(expect, update);
                System.out.println(Thread.currentThread().getName() + "开柜门：" + isOpen);
                // 省略 偷试题的操作
                boolean isClose = OPEN_OR_CLOSE.compareAndSet(OPEN_OR_CLOSE.get(), expect);
                System.out.println(Thread.currentThread().getName() + "关柜门：" + isClose);
                System.out.println(Thread.currentThread().getName() + "偷题是否成功" + (isOpen && isClose));
            }
        }, "zhangsan");

        Thread lisi = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 张三使手段，确保自己先执行完，真实场景可能用其它的手段
                    zhangsan.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "打开之前门状态为：" + OPEN_OR_CLOSE.get());
                boolean isOpen = OPEN_OR_CLOSE.compareAndSet(expect, update);
                System.out.println(Thread.currentThread().getName() + "打开之后门状态为：" + OPEN_OR_CLOSE.get() + ", " + Thread.currentThread().getName() + "开柜门是否成功：" + isOpen);
            }
        }, "lisi");
        zhangsan.start();
        lisi.start();
    }
}

